﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmHeDaoTao : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo biến

        public string maHeDaoTao = "";
        HeDaoTao_BLL hdt_bll = new HeDaoTao_BLL();
        DataTable dt;
        TextMessageBox box = new TextMessageBox();
        private bool isOK = false;
        #endregion

        #region LoadForm

        public frmHeDaoTao()
        {
            InitializeComponent();
        }

        private void frmHeDaoTao_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region Các chức năng

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        #endregion

        #region Sự kiện hệ thống

        private void frmHeDaoTao_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region Hàm viết thêm
        private void UpdateData()
        {
            if (checkValid())
            {
                HeDaoTao_Public hdt_public = new HeDaoTao_Public();
                if (maHeDaoTao == "")
                {
                    if (checkTenInsert())
                    {
                        hdt_public.MaHe = box.AutoID("HDT", 3, hdt_bll.LayDuLieu());
                        hdt_public.TenHeDaoTao =box.toUpper(txtTenHe.Text);
                        hdt_public.GhiChu = txtGhiChu.Text;

                        hdt_bll.Insert(hdt_public, hdt_public, hdt_public);

                        SetInterface();
                        CloseForm();
                    }
                }
                else
                {
                    if (checkTenUpdate())
                    {
                        hdt_public.MaHe = txtMaHe.Text.Trim();
                        hdt_public.TenHeDaoTao =box.toUpper(txtTenHe.Text);
                        hdt_public.GhiChu = txtGhiChu.Text;

                        hdt_bll.Update(hdt_public, hdt_public, hdt_public);

                        SetInterface();
                        CloseForm();
                    }
                }

            }
        }

        private void CloseForm()
        {
            if (isOK == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }

        private void LoadData()
        {
            if (maHeDaoTao != "")
            {
                dt = hdt_bll.TaoBang(box.sqlCheckExist("DM_HeDaoTao", "MaHe", maHeDaoTao));
                DataRow dr = dt.Rows[0];
                txtMaHe.Text = maHeDaoTao;
                txtTenHe.Text = dr["TenHeDaoTao"].ToString();
                txtGhiChu.Text = dr["GhiChu"].ToString();
            }
        }
        private void SetInterface()
        {
            txtMaHe.Text = "Mã tự sinh";
            txtTenHe.Text = "";
            txtGhiChu.Text = "";
            maHeDaoTao = "";
            txtTenHe.Focus();
        }
        private bool checkValid()
        {
            //if (txtMaHe.Text.Trim() == "")
            //{
            //    XtraMessageBox.Show("Bạn chưa nhập Mã hệ đào tạo.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    txtMaHe.Focus();
            //    return false;
            //}
            if (txtTenHe.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên hệ đào tạo.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTenHe.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            dt = hdt_bll.TaoBang(box.sqlCheckExist("DM_HeDaoTao", "TenHeDaoTao", txtTenHe.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTenHe.Text), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenHe.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenUpdate()
        {
            dt = hdt_bll.TaoBang(box.sqlCheckExist("DM_HeDaoTao", "TenHeDaoTao", txtTenHe.Text.Trim()));
            if (dt.Rows.Count +1> 1)
            {
                XtraMessageBox.Show(box.Exist(txtTenHe.Text), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenHe.Focus();
                return false;
            }
            return true;
        }
        #endregion
    }
}